# -*- coding: utf-8 -*-
"""
Created on Thu Oct 29 11:43:57 2020

@author: Administrator
"""

import requests
import pandas as pd
import matplotlib.pyplot as plt





#def draw_data(in_weather):
    

def control_data():
    
    #control data
    start_time='2019'+'%24'+'11'+'%24'+'01'+'%24'+'00'+'%3A'+'00'+'%3A'+'00'
    end_time='2020'+'%24'+'03'+'%24'+'31'+'%24'+'00'+'%3A'+'00'+'%3A'+'00'
    
    query_control_data='http://114.215.46.56:18817/v1_0_0/log?pageNumber=0&pageSize=100000&access_token=5f9a24fa1db334410000006f&company_code=000059000001&start_time='+start_time+'&end_time='+end_time+'&log_type=0&user_id=5dcd0fa98fab513500000021&key_name=%7B%22station_name%22%3A%22%22%7D'
    control_records = requests.get(query_control_data)
    control_records.encoding = 'utf-8'
    
    
    
    #print('返回结果:%s'%rep.json())
    
    control_datas=control_records.json()
    
    #print(type(datas))
    
    control_total_data=control_datas["total"]
    control_data_rows=control_datas["rows"]
    
    set_total=0
    
    set_temp=0
    set_fix_temp=0
    set_time_slot=0
    set_strategy=0
    set_Opening=0
    set_hz=0
    set_xhb_qiting=0
    set_xhb_celuexuanze=0
    set_ercigongya=0
    set_ercihuiya=0
    wastegate=0
    add_water=0
    
    
    
    for control_one_rows in control_data_rows:
        control_oper_time=control_one_rows["oper_time"]
        control_station_name=control_one_rows["station_name"]
        control_data_name=control_one_rows["data_name"]
        control_target_value=control_one_rows["target_value"]
        control_handle_result=control_one_rows["handle_result"]
        control_user_name=control_one_rows["user_name"]
        
     

        if control_station_name=='盛世华安低区' and control_handle_result=='1' :
           
            set_total=set_total+1          
            
            if '温度设定' in control_data_name:
                set_temp=set_temp+1
                continue    
            if '分时段修正温度' in control_data_name :
                set_fix_temp=set_fix_temp+1
                continue    
            
            if '分时段时间' in control_data_name :
                set_time_slot=set_time_slot+1
               
                continue   
            
            
            if '一网阀控制策略选择' in control_data_name :
                set_strategy=set_strategy+1 
               
                continue   
            
            
            if '一网阀开度给定' in control_data_name :
                set_Opening=set_Opening+1
                  
                continue   
            
            if '循环泵频率设定' in control_data_name :
                set_hz=set_hz+1
               
                continue   
            
            
            if '泄压阀' in control_data_name :
                wastegate=wastegate+1
               
                continue   
            
            if '补水泵' in control_data_name :
                add_water=add_water+1
                  
                continue   
            
            if '循环泵启停控制' in control_data_name :
                set_xhb_qiting=set_xhb_qiting+1
                 
                continue   
            
            if '循环泵控制策略选择' in control_data_name :
                set_xhb_celuexuanze=set_xhb_celuexuanze+1
                continue 
            
            if '二次供压设定' in control_data_name :
                set_ercigongya=set_ercigongya+1
              
              
                continue 
            if '二网回压设定' in control_data_name :
                set_ercihuiya=set_ercihuiya+1
                
                  
                  
                print(control_oper_time)
                print(control_station_name)
                print(control_data_name)
                print(control_target_value)
                print(control_handle_result)
                print(control_user_name,'\n')            
                print('-------------tail-------------------\n\n',)
                 
                continue 
            
         
            
            
          
            
               
                
    
    print('set_total:',set_total)
    print('set_temp:',set_temp)
    print('set_fix_temp:',set_fix_temp)
    print('set_time_slot:',set_time_slot)
    print('set_strategy:',set_strategy)
    print('set_Opening:',set_Opening)
    print('set_hz:',set_hz)
    print('set_xhb_qiting:',set_xhb_qiting)
    print('set_xhb_celuexuanze:',set_xhb_celuexuanze)
    print('set_ercigongya:',set_ercigongya)
    print('set_ercihuiya:',set_ercihuiya)
    print('wastegate:',wastegate)
    print('add_water:',add_water)
    
    
    
    
    
    query_control_data='http://api.station.heating.lanyueyun.com/v1_0_0/hourDatas?access_token=5f9bc4cff90ee13900000083&_id=5dc0dc39685a490001f8b83a&tag_id=20&start_time=2020%2409%2430%2417%3A31%3A00&end_time=2020%2410%2408%2417%3A31%3A00&level=3'
    control_records = requests.get(query_control_data)
    control_records.encoding = 'utf-8'   
    
    control_datas=control_records.json()
    #print('二网供温')
    #print(control_datas)
    
    #Request URL: http://api.station.heating.lanyueyun.com/v1_0_0/hourDatas?access_token=5f9bc4cff90ee13900000083&_id=5dc0dc39685a490001f8b83a&tag_id=20&start_time=2020%2409%2430%2417%3A31%3A00&end_time=2020%2410%2408%2417%3A31%3A00&level=3    


        

def weather_data():
    #weather  data  
    start_time='2020'+'%24'+'01'+'%24'+'07'+'%24'+'00'+'%3A'+'00'+'%3A'+'00'
    end_time='2020'+'%24'+'01'+'%24'+'10'+'%24'+'00'+'%3A'+'00'+'%3A'+'00'
    query_weather_data='http://114.215.46.56:18817/v1_0_0/weatherReport?access_token=5f9a24fa1db334410000006f&company_code=000059000001&start_hour='+start_time+'&end_hour='+end_time
    #query_weather_data='http://114.215.46.56:18817/v1_0_0/weatherReport?access_token=5f9a24fa1db334410000006f&company_code=000059000001&start_hour=2020%2410%2428%2400%3A00%3A00&end_hour=2020%2410%2429%2400%3A00%3A00'
    
    weather_records = requests.get(query_weather_data)
    weather_records.encoding = 'utf-8'
    
    
    weather_datas=weather_records.json()
    
    print(weather_datas)

    
    plt.rcParams['font.sans-serif'] = ['SimHei']  #显示中文
    plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
    weather_dataframe=pd.DataFrame(weather_datas)
    print(type(weather_dataframe))
    print(weather_dataframe)
    
    weather_dataframe.plot(x='date', y='temp')
    weather_dataframe.plot(x='date', y='img')
    
    plt.show()
    

def room_temperature():
    start_time='2019-11-01+00:00:00'
    end_time='2019-12-31+23:59:59' 
    
    
    #1号楼1单元的 温度计，2169336 没数据
    list_dataid=['2166715','2166803','2166799','2166811','2166823','2166807','2166819','2168062','2166719']
    dataid= '2166719'
    
    
    query_room_temperature='http://api.community.heating.lanyueyun.com/v1/datas/getDataHistory?access_token=5f9a3593c3c3393800000017&data_id='+dataid+'&start_time='+start_time+'&end_time='+end_time


    room_temperature = requests.get(query_room_temperature)
    room_temperature.encoding = 'utf-8'
    
    
    room_temperature_data=room_temperature.json()
    
    room_temperature_data_value=room_temperature_data["result"]["data_value"]
    
    print(room_temperature_data_value)
    
    print('\n')
    room_temperature_data_time=room_temperature_data["result"]["data_time"]
    
    print(room_temperature_data_time)
    
    temp_frame = pd.DataFrame({'temperature_time':room_temperature_data_time,'temperature':room_temperature_data_value})
    
    print(temp_frame.head(60))
    
    
    temp_frame[(temp_frame.temperature_time.find(':00:00'))]
    
    #temp_frame=temp_frame.temperature_time.isin([':00:00'])
    
    print(temp_frame.head(60))
    #print(room_temperature_data)
    
    
    

if __name__ == '__main__':
    
    control_data()
    #weather_data()
    #room_temperature()
    